﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace UtilZ.Dotnet.WindowsDesktopEx.WPF
{
    /// <summary>
    /// IconControl控件
    /// </summary>
    public class IconControl : Control
    {
        /// <summary>
        /// Fill依赖属性
        /// </summary>
        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register(nameof(Fill), typeof(Brush), typeof(IconControl),
                new FrameworkPropertyMetadata(Brushes.Transparent, new PropertyChangedCallback(OnPropertyChangedCallback)));

        /// <summary>
        /// Stroke依赖属性
        /// </summary>
        public static readonly DependencyProperty StrokeProperty =
            DependencyProperty.Register(nameof(Stroke), typeof(Brush), typeof(IconControl),
                new FrameworkPropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChangedCallback)));

        /// <summary>
        /// StrokeThickness依赖属性
        /// </summary>
        public static readonly DependencyProperty StrokeThicknessProperty =
            DependencyProperty.Register(nameof(StrokeThickness), typeof(double), typeof(IconControl),
                new FrameworkPropertyMetadata(0d, new PropertyChangedCallback(OnPropertyChangedCallback)));




        /// <summary>
        /// Text依赖属性
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register(nameof(Text), typeof(string), typeof(IconControl),
                new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnPropertyChangedCallback)));

        /// <summary>
        /// IsChecked依赖属性
        /// </summary>
        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register(nameof(IsChecked), typeof(bool), typeof(IconControl),
                new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnPropertyChangedCallback)));

        /// <summary>
        /// IsPressed依赖属性
        /// </summary>
        public static readonly DependencyProperty IsPressedProperty =
            DependencyProperty.Register(nameof(IsPressed), typeof(bool), typeof(IconControl),
                new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPropertyChangedCallback)));




        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selfControl = (IconControl)d;
            if (e.Property == IsCheckedProperty)
            {
                selfControl.OnRaiseIsCheckedChanged((bool)e.OldValue, (bool)e.NewValue);
            }

        }





        /// <summary>
        /// Gets or sets the System.Windows.Media.Brush that specifies how the shape's interior is painted.
        /// 返回结果:A System.Windows.Media.Brush that describes how the shape's interior is painted. The default is null.
        /// </summary>
        public Brush Fill
        {
            get
            {
                return (Brush)base.GetValue(FillProperty);
            }
            set
            {
                base.SetValue(FillProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the System.Windows.Media.Brush that specifies how the System.Windows.Shapes.Shape outline is painted.
        /// 返回结果: A System.Windows.Media.Brush that specifies how the System.Windows.Shapes.Shape outline is painted. The default is null.
        /// </summary>
        public Brush Stroke
        {
            get
            {
                return (Brush)base.GetValue(StrokeProperty);
            }
            set
            {
                base.SetValue(StrokeProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the width of the System.Windows.Shapes.Shape outline.
        /// 返回结果: The width of the System.Windows.Shapes.Shape outline.
        /// </summary>
        [TypeConverter(typeof(LengthConverter))]
        public double StrokeThickness
        {
            get
            {
                return (double)base.GetValue(StrokeThicknessProperty);
            }
            set
            {
                base.SetValue(StrokeThicknessProperty, value);
            }
        }

        /// <summary>
        /// 获取或设置文本
        /// </summary>
        public string Text
        {
            get
            {
                return (string)base.GetValue(TextProperty);
            }
            set
            {
                base.SetValue(TextProperty, value);
            }
        }

        /// <summary>
        /// 是否被选中
        /// </summary>
        public bool IsChecked
        {
            get
            {
                return (bool)base.GetValue(IsCheckedProperty);
            }
            set
            {
                base.SetValue(IsCheckedProperty, value);
            }
        }

        /// <summary>
        /// 获取一个值,指示鼠标是否按下.按下则为 true；否则为 false。默认值为 false。
        /// </summary>
        [Browsable(false)]
        [Category("Appearance")]
        [ReadOnly(true)]
        public bool IsPressed
        {
            get
            {
                return (bool)base.GetValue(IsPressedProperty);
            }
            protected set
            {
                base.SetValue(IsPressedProperty, value);
            }
        }


        /// <summary>
        /// 在选中IconControl时发生。
        /// </summary>
        [Category("Behavior")]
        public event EventHandler<IconControlIsCheckedChangedArgs> IsCheckedChanged;

        private void OnRaiseIsCheckedChanged(bool oldIsChecked, bool newIsChecked)
        {
            this.IsCheckedChanged?.Invoke(this, new IconControlIsCheckedChangedArgs(oldIsChecked, newIsChecked));
        }




        /// <summary>
        /// 构造函数初始化
        /// </summary>
        public IconControl()
            : base()
        {

        }

        /// <summary>
        /// 重写OnPreviewMouseLeftButtonDown
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseLeftButtonDown(e);

            if (!e.Handled)
            {
                this.IsChecked = !this.IsChecked;
            }
        }

        /// <summary>
        /// 重写OnMouseDown
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            this.IsPressed = true;
        }

        /// <summary>
        /// 重写OnMouseUp
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);

            this.IsPressed = false;
        }
    }

    /// <summary>
    /// IconControl.IsCheckedChanged事件参数
    /// </summary>
    public class IconControlIsCheckedChangedArgs : EventArgs
    {
        /// <summary>
        /// 旧值
        /// </summary>
        public bool OldIsChecked { get; private set; }

        /// <summary>
        /// 新值
        /// </summary>
        public bool NewIsChecked { get; private set; }

        /// <summary>
        /// 构造函数初始化
        /// </summary>
        /// <param name="oldIsChecked">旧值</param>
        /// <param name="newIsChecked">新值</param>
        public IconControlIsCheckedChangedArgs(bool oldIsChecked, bool newIsChecked)
        {
            this.OldIsChecked = oldIsChecked;
            this.NewIsChecked = newIsChecked;
        }
    }
}
